package com.kevin.ymqquan.activity;

import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;
import android.app.Dialog;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;

import com.kevin.ymqquan.R;
import com.kevin.ymqquan.constants.Constants;
import com.kevin.ymqquan.custom.CustomToast;
import com.kevin.ymqquan.httppost.Helper;
import com.kevin.ymqquan.httppost.HttpPostService;

public class ConDetailActivity extends Activity {

	private ImageView iv_con_detail_back;
	private EditText et_condetail_consume;
	private EditText et_condetail_remark;
	private Button btn_con_detail_submit;

	private String clubId;
	private String activeId;
	private String Ids;
	private Dialog dialog;
	
	private String money = "0";

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_con_detail);
		init();

		Intent i = getIntent();
		activeId = i.getStringExtra("activeId");
		Ids = i.getStringExtra("Ids");
		clubId = i.getStringExtra("clubId");
	}

	private void init() {
		// TODO Auto-generated method stub
		iv_con_detail_back = (ImageView) findViewById(R.id.iv_con_detail_back);
		iv_con_detail_back.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				backIntent();
			}
		});
		et_condetail_consume = (EditText) findViewById(R.id.et_condetail_consume);
		et_condetail_remark = (EditText) findViewById(R.id.et_condetail_remark);
		btn_con_detail_submit = (Button) findViewById(R.id.btn_con_detail_submit);
		btn_con_detail_submit.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub

				int con = 0;
				try {
					con = Integer.parseInt(et_condetail_consume.getText()
							.toString());
				} catch (Exception e) {
					e.printStackTrace();
				}

				if ("".equals(et_condetail_consume.getText().toString())) {
					CustomToast.CustomActivityToast(ConDetailActivity.this,
							"请输入金额！", -80);
				} else if (con <= 0) {
					CustomToast.CustomActivityToast(ConDetailActivity.this,
							"输入的金额不合法！", -80);
				} else {
					if (Helper.checkConnection(ConDetailActivity.this)) {
						dialog = CustomToast.createLoadingDialog(
								ConDetailActivity.this, "数据获取中...");
						dialog.setCancelable(true);
						dialog.show();
						ConDetailAsyTask policyTask = new ConDetailAsyTask();
						policyTask.execute();

					} else {
						CustomToast.CustomActivityToast(ConDetailActivity.this,
								"网络连接异常，请检查您的网络", -80);
					}
				}
			}
		});
	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK) {
			backIntent();
		}
		return false;
	}

	private void backIntent() {
		Intent intent = new Intent(ConDetailActivity.this,
				ActiveDetailActivity.class);
		intent.putExtra("consume", money);
		setResult(RESULT_OK, intent);
		ConDetailActivity.this.finish();
	}

	class ConDetailAsyTask extends AsyncTask<String, Object, String> {

		@Override
		protected String doInBackground(String... params) {
			// TODO Auto-generated method stub

			String remark = et_condetail_remark.getText().toString();
			// sendReplyText = Base64.encodeToString(sendReplyText.getBytes(),
			// Base64.NO_WRAP);

			String param = "{\"method\":\"userCon\",\"Ids\":\""
					+ Ids
					+ "\",\"activeId\":\""
					+ activeId
					+ "\",\"activeId\":\""
					+ activeId
					+ "\",\"clubId\":\""
					+ clubId
					+ "\",\"consume\":\""
					+ Integer.parseInt(et_condetail_consume.getText()
							.toString()) + "\",\"remark\":\"" + remark + "\"}";
			String replyList = "";
			try {
				replyList = HttpPostService.PostJsonString(
						Constants.ServerAddr, param);
			} catch (Exception e) {
				e.printStackTrace();
			}
			Log.i("kevinlog", param);
			Log.i("kevinlog", replyList);
			return replyList;
		}

		@Override
		protected void onPostExecute(String result) {
			// TODO Auto-generated method stub
			super.onPostExecute(result);
			// Log.i("kevinlog", result);
			JSONObject jsonObject;
			try {
				jsonObject = new JSONObject(result);
				String resultType = jsonObject.getString("status");
				if ("0".equals(resultType)) {
					CustomToast.CustomActivityToast(ConDetailActivity.this,
							"消费成功", 0);
					// isJoin = 1;
					money = et_condetail_consume.getText().toString();
					backIntent();
					// ActiveApplyActivity.this.finish();
				} else if ("100".equals(resultType)) {
					CustomToast.CustomActivityToast(ConDetailActivity.this,
							"服务器异常，请稍后重试", 0);
				} else if ("200".equals(resultType)) {
					CustomToast.CustomActivityToast(ConDetailActivity.this,
							"本人消费余额不足", 0);
				} else if ("300".equals(resultType)) {
					CustomToast.CustomActivityToast(ConDetailActivity.this,
							"带人消费余额不足", 0);
				}

			} catch (JSONException e) {
				// TODO Auto-generated catch block
				Log.i("kevinlog", "error");
				e.printStackTrace();
			}

			dialog.dismiss();
		}

	}

}
